# -*- coding: utf-8 -*-
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte <dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.
'''
Copy of the thuban/Thuban/UI/main.py file of the thuban application

Author : Didrik Pinte <dpinte@dipole-consulting.com>::
Lastest modif by : $Author: dpinte $
Version : $Revision: 40 $
File : $HeadURL: https://secure2.svnrepository.com/s_dpinte/hydroplatform/src/python/trunk/hydroplatform/ui/main.py $
Date : $Date: 2009-01-21 14:27:24 +0000 (Wed, 21 Jan 2009) $
    '''
__revision__ = "$Revision: 40 $"

import os
import sys
import getopt
import logging

# FIXME : move the log file to a specific place
if sys.platform == 'win32':
    logfile = 'hydroplatform.log'
else:
    logfile = 'hydroplatform.log'


logging.basicConfig(filename=logfile, 
                    filemode='a', 
                    format='%(asctime)s %(levelname)s %(message)s',
                    level= logging.DEBUG)

from hydroplatform.ui.application import HydroPlatformApplication
import Thuban

class options:
    attribute_editing_enabled = False


def main():
    """Instantiate the application object and run the application"""

    if verify_versions():
        app = HydroPlatformApplication(redirect=False)
        opts, args = getopt.getopt(sys.argv[1:], '',
                                   ['enable-attribute-editing'])
        for optchar, value in opts:
            if optchar == '--enable-attribute-editing':
                options.attribute_editing_enabled = True
            else:
                print >>sys.stderr, "Unknown option", optchar

        # If there was a non-flag argument it's the name of a thuban
        # file.
        if args:
            app.OpenSession(args[0])

        app.MainLoop()
        # sys.excepthook is set in ThubanApplication.OnInit()
        sys.excepthook = sys.__excepthook__


def verify_versions():
    """Check some library versions.

        Print a message containing any libraries which are wrong.
        Return True if everything is OK, otherwise False.
        """

    errors = Thuban.version.verify_versions()

    if len(errors) > 0:
        msg = " The following version errors were detected:"

        for e in errors:
            msg += "\n     " + e

        print "\n*******************************************************"
        print msg
        print "*******************************************************\n"

        return False

    return True

if __name__ == '__main__':
    main()
